#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <set>
using namespace std;
class Solution
{
public:
    // num[Al] ... num[mid] ... num[Bh]
    // A : Al ~ mid ;; B: mid+1 ~ Bh
    int cntReserverseNum(vector<int> &nums, int low, int high)
    {
        if (low >= high)
            return 0;
        int mid = (low + high) / 2, cnt = 0, i, j, k;
        cnt += cntReserverseNum(nums, low, mid);
        cnt += cntReserverseNum(nums, mid + 1, high);
        vector<int> tmpVector(high - low + 1);
        for (i = low, j = mid + 1, k = 0; i <= mid && j <= high;)
        {
            if (nums[i] <= nums[j])
            {
                tmpVector[k++] = nums[i++];
            }
            else
            {
                cnt += mid - i + 1;
                tmpVector[k++] = nums[j++];
            }
        }
        while (i <= mid)
        {
            tmpVector[k++] = nums[i++];
        }
        while (j <= high)
        {
            tmpVector[k++] = nums[j++];
        }
        for (int i = low, k = 0; i <= high; ++i, ++k)
        {
            nums[i] = tmpVector[k];
        }
        return cnt;
    }
    int reversePairs(vector<int> &nums)
    {
        return cntReserverseNum(nums, 0, nums.size() - 1);
    }
};